package org.eoti.rei.ndfcmd;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import org.eoti.io.xls.XLSReader;
import org.eoti.io.xls.XLSColumn;

import java.util.*;

public class ListCmd
	extends NDFBrowserCommand
{
	public String[] commands()
	{
		return new String[]{"list","ls"};
	}

	public void process( List<String> commandLine)
			throws Exception
	{
		XLSReader reader = browser.getReader();
		if(reader == null)
		{
			format("No data loaded. Please try 'load filename'.\n");
			return;
		}
		boolean showCity = true;
		boolean showIndex = true;
		boolean showEquity = false;
		boolean showAuction = false;
		for(String command : commandLine)
		{
			if(command.equalsIgnoreCase("equity"))
				showEquity = true;

			if(command.equalsIgnoreCase("auction"))
				showAuction = true;

			if(command.equalsIgnoreCase("-city"))
				showCity = false;

			if(command.equalsIgnoreCase("-index"))
				showIndex = false;
		}

		format("\n");
		XLSColumn oweCol = reader.getColumn("Foreclosure Judgement Amt");
		XLSColumn worthCol = reader.getColumn("Total Value");
		for(int rowNum : reader.rows())
		{
			if(showIndex)
				format("%3d. ", rowNum);

			format("%-6s %-20s ", reader.getColumn(0).getString(rowNum), reader.getColumn(1).getString(rowNum));

			if(showCity)
				format("%-30s ", reader.getColumn(2).getString(rowNum));

			if(showAuction)
				format("[%1$ta %1$td %1$tb %1$tY %1$tI:%1$tM %1$Tp] ", browser.getAuctionDateTime(rowNum));

			if(showEquity)
			{
				double worth = (Double)(worthCol.getValue(rowNum));
				double owe = (Double)(oweCol.getValue(rowNum));
				double equity = worth-owe;
				double percent = (equity/worth)*100.0;
				format("(%.2f%% / $%,.2f) ", percent, equity);
			}

			format("\n");
		}
	}

	public void displayHelp()
	{
		format("ls [options]: list current houses\n");
		format("list [options]: list current houses\n");
		format("options:\n");
		format("\tequity: display equity\n");
		format("\tauction: display auction date/time\n");
		format("\t-city: don't display city,state,zip\n");
		format("\t-index: don't show house index\n");
	}
}
